<!DOCTYPE>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Netty-Websocket</title>
    <script type="text/javascript">

        var socket;
        if (!window.WebSocket) {
            window.WebSocket = window.MozWebSocket;
        }
        if (window.WebSocket) {
            socket = new WebSocket("ws://127.0.0.1:12345/ws");
            socket.onmessage = function (event) {
                var ta = document.getElementById('responseText');
                ta.value += event.data + "\r\n";
            };
            socket.onopen = function (event) {
                var ta = document.getElementById('responseText');
                ta.value = "Netty-WebSocket服务器。。。。。。连接  \r\n";
            };
            socket.onclose = function (event) {
                var ta = document.getElementById('responseText');
                ta.value = "Netty-WebSocket服务器。。。。。。关闭 \r\n";
            };
        } else {
            alert("您的浏览器不支持WebSocket协议！");
        }

        function send(message) {
            if (!window.WebSocket) {
                return;
            }
            if (socket.readyState === WebSocket.OPEN) {
                socket.send(message);
            } else {
                alert("WebSocket 连接没有建立成功！");
            }

        }

        // 打开一个 web socket
        var ws = new WebSocket("wss://socket.nicemoe.cn");

        ws.onopen = function () {
            // let data = {};
            // data.type = 1017;
            // data.echo = 153166341
            // ws.send(JSON.stringify(data));
            // // console.log("数据发送中..." + test);
        };

        ws.onmessage = function (evt) {
            var received_msg = evt.data;
            // if (received_msg.indexOf("\"server\": \"剑胆琴心\"") > -1 || received_msg.indexOf("\"type\": \"官方新闻\"") > -1) {
            if ((received_msg.indexOf("\"type\": 2001") > -1 && received_msg.indexOf("\"server\": \"剑胆琴心\"") > -1) || received_msg.indexOf("\"type\": 2002") > -1) {
                send(received_msg);
                console.log("数据已接收..." + received_msg);
            }
        };

        ws.onclose = function () {
            // 关闭 websocket
            console.log("连接已关闭...");
        };

        function sendToApi(type, server) {
            if (!window.WebSocket) {
                return;
            }
            if (ws.readyState === WebSocket.OPEN) {
                ws.send(JSON.stringify(message));
            } else {
                alert("WebSocket 连接没有建立成功！");
            }

        }

        var flag = 1;
        window.setInterval(function () {

            if (socket.readyState === WebSocket.OPEN) {
                document.getElementById('health').value += "连接正常 " + new Date() + "\r\n";
                flag++;
            } else {
                location.reload();
            }
            if (flag > 20) {
                document.getElementById('health').value = "";
                flag = 0;
            }
        }, 10000)

    </script>
</head>
<body>
<form onSubmit="return false;">
    <label>ID</label><input type="text" name="uid" value="${uid!!}"/> <br/>
    <label>TEXT</label><input type="text" name="message" value="这里输入消息"/> <br/>
    <br/> <input type="button" value="发送ws消息"
                 onClick="send(this.form.uid.value+':'+this.form.message.value)"/>
    <hr color="black"/>
    <h3>服务端返回的应答消息</h3>
    <textarea id="responseText" style="width: 1024px;height: 300px;"></textarea>
    <textarea id="health" style="width: 1024px;height: 300px;"></textarea>
</form>
</body>
</html>